/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shattered2;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author 10135228
 */
public class PuzzleField {
    
    private HashMap<Integer,HashMap<Integer,PuzzlePiece>> data = null;
    private int minX = 0, maxX = 0, minY = 0, maxY = 0;
    private JPanel display = null;
    private ArrayList<Integer> used = new ArrayList<>();

    @SuppressWarnings("unchecked")
    public PuzzleField() {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream("save"));
            data = (HashMap<Integer,HashMap<Integer,PuzzlePiece>>)in.readObject();
            minX = in.readInt();
            maxX = in.readInt();
            minY = in.readInt();
            maxY = in.readInt();
            used = (ArrayList<Integer>)in.readObject();
        } catch (IOException ex) {
            System.out.println("Save not found.");
            data = new HashMap<>();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PuzzleField.class.getName()).log(Level.SEVERE, null, ex);
        } 
        finally{
            try {
                in.close();
            } catch (IOException|NullPointerException ex) {
                //Logger.getLogger(PuzzleField.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    

    
    public void setPieceAt(int x, int y, PuzzlePiece piece){
        //System.out.println("Setting: "+x+"-"+y);
        x=x-2+minX;
        y=y-2+minY;
        //System.out.println("Translated to: "+x+"-"+y);
        if(data.get(x)==null){
            data.put(x,new HashMap<Integer, PuzzlePiece>());
        }
        if(data.get(x).get(y)!=null){
            data.get(x).get(y).setUsed(false);
            used.remove((Object)data.get(x).get(y).getID());
        }
        if(piece != null){
            data.get(x).put(y, piece);   
            piece.setUsed(true);
            used.add(piece.getID());
        }else if(data.get(x).get(y)!=null){
            
            used.remove((Object)data.get(x).get(y).getID());
            data.get(x).remove(y);
        }

        minX = minX < x ? minX : x;
        maxX = maxX > x ? maxX : x;
        minY = minY < y ? minY : y;
        maxY = maxY > y ? maxY : y;
        display.repaint();
    }
    
    public PuzzlePiece getPieceAt(int x, int y){
        //System.out.println("Question for: "+x+"-"+y);
        x=x-2+minX;
        y=y-2+minY;

        if(data.get(x)==null){
            //System.out.println("Translated to: "+x+"-"+y);
            return null;
        }else{
            return data.get(x).get(y);
        }
    }


    void addDisplay(DrawPanel2 zoomx) {
        this.display = zoomx;
    }

    void save() {
                ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream("save"));
            out.writeObject(data);
            out.writeInt(minX);
            out.writeInt(maxX);
            out.writeInt(minY);
            out.writeInt(maxY);
            out.writeObject(used);
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    boolean isUsed(int id){
        return used.contains(id);
    }
}
